﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BellAssistant.Common.CommonHelper
{
        public static class EnumHelper
        {
            public static string GetDescription(this Enum obj)
            {
                return GetDescription(obj, false);
            }
            private static string GetDescription(this Enum obj, bool isTop)
            {
                if (obj == null)
                {
                    return string.Empty;
                }
                try
                {
                    Type _enumType = obj.GetType();
                    DescriptionAttribute dna = null;
                    if (isTop)
                    {
                        dna = (DescriptionAttribute)Attribute.GetCustomAttribute(_enumType, typeof(DescriptionAttribute));
                    }
                    else
                    {
                        FieldInfo fi = _enumType.GetField(Enum.GetName(_enumType, obj));

                        dna = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
                    }
                    if (dna != null && string.IsNullOrEmpty(dna.Description) == false)
                        return dna.Description;
                }
                catch
                {
                    return string.Empty;
                }
                return obj.ToString();
            }
        }
}
